package me.miencraft.plugin.worldinfo;

import org.bukkit.World;
import org.bukkit.block.Biome;

/**
 * @author langle__
 * @version 1.0
 */
public class WorldInfoUtil {

    public static String formatFrom(TimeType type) {
        return WorldInfo.timeMap.get(type);
    }

    public static String formatFrom(WeatherType type) {
        return WorldInfo.weatherMap.get(type);
    }

    public static String formatFrom(Biome biome) {
        String name = WorldInfo.biomeMap.get(biome);
        if (name == null) {
            name = biome.name();
        }
        return name;
    }

    public static String formatFrom(MoonPhase moonPhase) {
        return WorldInfo.moonPhaseMap.get(moonPhase);
    }

    public static String timeFormat(long dayTime) {
        int normalizedTick = (int) (dayTime % 24000);
        int totalMinutes = normalizedTick * 1440 / 24000;
        totalMinutes = (totalMinutes + 6 * 60) % (24 * 60);

        int hours = totalMinutes / 60;
        int minutes = totalMinutes % 60;

        return String.format("%02d:%02d", hours, minutes);
    }

    public static int getCurrentWeatherDuration(World world) {
        WeatherType type = WeatherType.fromWorldWeather(world);
        if (type == WeatherType.CLEAR) {
            if (WorldInfo.IS_SERVER_AT_LEAST_1_16_4) {
                int duration = world.getClearWeatherDuration();
                if (duration <= 0) {
                    duration = world.getWeatherDuration();
                }
                return duration;
            } else {
                return world.getWeatherDuration();
            }
        } else if (type == WeatherType.RAIN) {
            return world.getWeatherDuration();
        } else if (type == WeatherType.THUNDER) {
            return world.getThunderDuration();
        }
        return world.getWeatherDuration();
    }

    /**
     * 将 tick 转换为格式化的时间字符串 (HH:mm:ss)
     *
     * @param tick 表示游戏中的时间刻度
     * @return 格式为 "HH:mm:ss" 的字符串
     */
    public static String formatTickToTime(long tick) {
        TimeComponents components = calculateTimeComponents(tick);
        return String.format("%02d:%02d:%02d", components.hours, components.minutes, components.seconds);
    }

    /**
     * 将 tick 转换为指定格式的时间字符串
     *
     * @param tick 表示游戏中的时间刻度
     * @param isFullFormat 是否使用完整格式 (HH:mm:ss)，否则返回分秒格式 (mm:ss)
     * @return 格式为 "mm:ss" 或者 "HH:mm:ss" 的字符串
     */
    public static String formatTickToCustomString(long tick, boolean isFullFormat) {
        TimeComponents components = calculateTimeComponents(tick);
        if (isFullFormat) {
            return String.format("%02d:%02d:%02d", components.hours, components.minutes, components.seconds);
        } else {
            int totalMinutes = components.hours * 60 + components.minutes;
            return String.format("%02d:%02d", totalMinutes, components.seconds);
        }
    }

    /**
     * 计算 tick 对应的小时部分
     *
     * @param tick 表示游戏中的时间刻度
     * @return 小时数
     */
    public static int getHoursFromTick(long tick) {
        return calculateTimeComponents(tick).hours;
    }

    /**
     * 计算 tick 对应的分钟部分
     *
     * @param tick 表示游戏中的时间刻度
     * @return 分钟数
     */
    public static int getMinutesFromTick(long tick) {
        TimeComponents components = calculateTimeComponents(tick);
        return (components.hours * 60) + components.minutes;
    }

    /**
     * 计算 tick 对应的秒数
     *
     * @param tick 表示游戏中的时间刻度
     * @return 秒数
     */
    public static int getSecondsFromTick(long tick) {
        TimeComponents components = calculateTimeComponents(tick);
        return (components.hours * 60 * 60) + (components.minutes * 60) + components.seconds;
    }

    /**
     * 封装时间组件（小时、分钟、秒）
     */
    private static class TimeComponents {
        private final int hours;
        private final int minutes;
        private final int seconds;

        public TimeComponents(int hours, int minutes, int seconds) {
            this.hours = hours;
            this.minutes = minutes;
            this.seconds = seconds;
        }
    }

    /**
     * 公共方法：将 tick 换算为小时、分钟、秒
     *
     * @param tick 表示游戏中的时间刻度
     * @return 包含小时、分钟、秒的 {@link TimeComponents} 对象
     */
    private static TimeComponents calculateTimeComponents(long tick) {
        long totalMillis = tick * 50; // 每个 tick 是 50ms
        int hours = (int) (totalMillis / (60 * 60 * 1000)) % 24;
        int minutes = (int) ((totalMillis / (60 * 1000)) % 60);
        int seconds = (int) ((totalMillis / 1000) % 60);
        return new TimeComponents(hours, minutes, seconds);
    }

}

